<template>
  <el-drawer size="1000px" :title="dialogTitle" :visible.sync="dialogVisible" @close="handleClose">
    <div style="height: 100%;display: flex;padding: 0 20px 20px 20px;flex-direction:column">
      <el-form
        ref="submitForm"
        v-loading="formLoading"
        style="flex: 1"
        :model="submitForm"
        :rules="submitFormRules"
        label-width="120px"
        label-position="right"
        class="submit-form"
      >
        <div class="info_group">
          <div class="info_section" />
          <div class="info_title">基本信息</div>
        </div>
        <el-row>
          <el-col :span="12">
            <el-form-item label="员工名称" prop="employeeName">
              <el-input v-model="submitForm.employeeName" placeholder="请输入员工名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号" prop="phoneNum">
              <el-input v-model="submitForm.phoneNum" placeholder="请输入手机号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="身份证号码" prop="idNo">
              <el-input v-model="submitForm.idNo" placeholder="请输入身份证号码" @input="idNoInput" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="sex">
              <dict-select v-model="submitForm.sex" dict-key="sex" placeholder="请选择性别" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出生日期" prop="birthDay">
              <el-date-picker
                v-model="submitForm.birthDay"
                style="width: 100%"
                type="date"
                placeholder="请选择出生日期"
                value-format="yyyy-MM-dd"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="家庭住址" prop="homeAddress">
              <el-input
                v-model="submitForm.homeAddress"
                placeholder="请输入家庭住址"
                type="textarea"
                :autosize="{ minRows: 2}"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <div class="info_group">
          <div class="info_section" />
          <div class="info_title">入职信息</div>
        </div>
        <el-row>
          <el-col :span="12">
            <el-form-item label="入职日期" prop="hireDate">
              <el-date-picker
                v-model="submitForm.hireDate"
                style="width: 100%"
                type="date"
                placeholder="请选择入职日期"
                value-format="yyyy-MM-dd"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="职位状态" prop="positionState">
              <dict-select v-model="submitForm.positionState" dict-key="position_state" placeholder="请选职位状态" />
            </el-form-item>
          </el-col>
        </el-row>
        <div class="info_group">
          <div class="info_section" />
          <div class="info_title">紧急联系人</div>
        </div>
        <el-row>
          <el-col :span="12">
            <el-form-item label="紧急联系人名称" prop="emergencyContactName">
              <el-input v-model="submitForm.emergencyContactName" placeholder="请输入紧急联系人名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="紧急联系人手机" prop="emergencyContactNum">
              <el-input v-model="submitForm.emergencyContactNum" placeholder="请输入紧急联系人手机号" />
            </el-form-item>
          </el-col>
        </el-row>
        <div class="info_group">
          <div class="info_section" />
          <div class="info_title">银行卡</div>
        </div>
        <el-col :span="12">
          <el-form-item label="银行卡号" prop="bankCardNo">
            <el-input v-model="submitForm.bankCardNo" placeholder="请输入银行卡号" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="银行卡开户行" prop="openingBank">
            <el-input v-model="submitForm.openingBank" placeholder="请输入银行卡开户行" />
          </el-form-item>
        </el-col>
      </el-form>
      <div style="display: flex">
        <el-button style="flex: 1" :loading="formLoading" @click="handleClose">取消</el-button>
        <el-button style="flex: 1" :loading="formLoading" type="primary" @click="handleSubmit">提交</el-button>
      </div>
    </div>
<!--    <span slot="footer" class="dialog-footer">
      <el-button :loading="formLoading" type="primary" @click="handleSubmit">提交</el-button>
      <el-button :loading="formLoading" @click="handleClose">取消</el-button>
    </span>-->
  </el-drawer>
</template>

<script>
import { employee_update, employee_save, employee_detail } from '@/api/base-data/employee'
import moment from 'moment'
/**
 * @author yizuomin
 * @date 2023/5/7 19:05
 **/
export default {
  name: 'DetailModule',
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formLoading: false,
      dialogVisible: this.visible,
      dialogTitle: '',
      submitForm: {
        id: '',
        employeeName: '',
        phoneNum: '',
        idNo: '',
        sex: '',
        birthDay: '',
        hireDate: moment().format('YYYY-MM-DD'),
        emergencyContactName: '',
        emergencyContactNum: '',
        positionState: '1',
        homeAddress: '',
        bankCardNo: '',
        openingBanck: ''
      },
      submitFormRules: {
        employeeName: [{ required: true, message: '请输入员工名称', trigger: 'change' }],
        phoneNum: [{ required: true, message: '请输入手机号码', trigger: 'change' }],
        idNo: [{ required: true, message: '请输入身份证号码', trigger: 'change' }],
        sex: [{ required: true, message: '请选择性别', trigger: 'change' }],
        birthDay: [{ required: true, message: '请选择出生日期', trigger: 'change' }],
        hireDate: [{ required: true, message: '请选择入职日期', trigger: 'change' }],
        positionState: [{ required: true, message: '请选择职位状态', trigger: 'change' }]
      }
    }
  },
  methods: {
    init(id) {
      if (!id) {
        this.dialogTitle = '新增员工'
        return
      }
      this.dialogTitle = '修改员工'
      this.formLoading = true
      employee_detail(id).then(resp => {
        if (resp.success) {
          this.submitForm = resp.data
        }
      }).finally(() => {
        this.formLoading = false
      })
    },
    handleSubmit() {
      this.$refs.submitForm.validate((valid) => {
        if (valid) {
          let request = employee_save
          if (this.submitForm.id) {
            request = employee_update
          }
          this.formLoading = true
          request(this.submitForm).then(resp => {
            if (resp.success) {
              this.$message.success('操作成功')
              this.handleClose()
              this.$refs.submitForm.resetFields()
            }
          }).finally(() => {
            this.formLoading = false
          })
        }
      })
    },
    handleClose() {
      this.$emit('close')
    },
    idNoInput(val) {
      if (val.length === 18) {
        const date = val.substring(6, 14)
        this.submitForm.birthDay = moment(date, 'YYYYMMDD').format('YYYY-MM-DD')
        var sexNumber = val[16]
        var number = sexNumber % 2
        if (number === 0) {
          this.submitForm.sex = '0'
        } else {
          this.submitForm.sex = '1'
        }
      }
    }
  }
}
</script>

<style scoped>

</style>
